import uuid

from django.db import models
from django.utils import timezone


class Privilege(models.Model):
    """
    套餐特权表
    """
    objects = models.Manager()

    name = models.CharField("特权名称", max_length=100, unique=True)

    class Meta:
        db_table = 'privilege'


class Package(models.Model):
    """
    AI 会话套餐表
    """
    objects = models.Manager()

    PERIOD_CHOICES = [
        ('monthly', '月度'),
        ('quarter', '季度'),
        ('yearly', '年度'),
    ]

    id = models.CharField(primary_key=True, default=uuid.uuid4, editable=False, max_length=255)
    name = models.CharField("套餐名", max_length=100)
    price = models.DecimalField("价格", max_digits=10, decimal_places=2)
    duration = models.CharField("时长类型", max_length=10, choices=PERIOD_CHOICES, default='monthly')
    privileges = models.ManyToManyField(Privilege, related_name="packages", blank=True)
    status = models.BooleanField("状态", default=True, help_text="是否启用")
    recommend = models.BooleanField("推荐", default=False, help_text="是否推荐")
    create_time = models.DateTimeField("创建时间", default=timezone.now)
    update_time = models.DateTimeField("修改时间", auto_now=True)

    class Meta:
        db_table = 'package'

    def duration_to_days(self):
        """
        将套餐时长类型转换为天数
        """
        if self.duration == 'monthly':
            return 30  # 月度，30天
        elif self.duration == 'quarter':
            return 90  # 季度，90天
        elif self.duration == 'yearly':
            return 365  # 年度，365天
        return 0  # 默认为0天

    def get_priority(self):
        """通过duration映射优先级"""
        return {
            'yearly': 3,
            'quarter': 2,
            'monthly': 1
        }[self.duration]
